package com.hjx.windowtool.util.test;

import java.util.Date;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class RobotHttpHandle {

	public static String cookie = "";

	public RobotHttpHandle() {

	}

	private HttpEntity initCookie(String url) {
		try {
			String baseURL = HttpHandle.getDomain(url);
			HttpHandle http = new HttpHandle();
			http.config.setRequestProperty("If-Modified-Since", new Date().toString());
			http.config.setRequestProperty("Cache-Control", "max-age=0");
			http.config.setRequestProperty("Upgrade-Insecure-Requests", "1");
			http.config.setKeepAlive(true);
			HttpEntity entity = http.Get(baseURL);
			System.out.println(entity.getCookie());
			String html = entity.getHtml();
			String temp = html.replace(" ", "");
			String jschl_vc = StringUtil.textCutCenter(temp, "jschl_vc\"value=\"", "\"");
			String pass = StringUtil.textCutCenter(temp, "pass\"value=\"", "\"");

			String funcCode = StringUtil.textCutCenter(html, "setTimeout(function(){", "f.submit();");

			funcCode = funcCode.replace("a.value", "a");
			funcCode = funcCode.replace("  ", " ");
			String[] tabs = funcCode.split("\n");
			funcCode = tabs[1];
			funcCode += "\r\nt=\"" + baseURL + "\";";
			funcCode += "\r\nr = t.match(/https?:\\/\\//)[0];";
			funcCode += "\r\nt = t.substr(r.length);";
			funcCode += "\r\nt = t.substr(0, t.length - 1);";
			funcCode += tabs[8];
			funcCode += "\r\n return a;";

			funcCode = "function jschl_answer(){\r\n" + funcCode + "\r\n}";

			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("js");
			engine.eval(funcCode);
			Invocable invocable = (Invocable) engine;
			Double jschl_answer = (Double) invocable.invokeFunction("jschl_answer");
			url = baseURL + "/cdn-cgi/l/chk_jschl?jschl_vc=" + jschl_vc + "&pass=" + pass + "&jschl_answer=" + jschl_answer.intValue();
			http.config.allowRedirects(false);
			System.out.println(url);
			Thread.sleep(3800l);
			http.config.setGzip(true);
			entity = http.Get(url);
			cookie = entity.getCookie();
			if (!cookie.contains("cf_clearance")) {
				return null;
			}
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public HttpEntity Get(String url) {
		if (cookie != null && !"".equals(cookie)) {
			loadCookie(url);
		}
		HttpHandle http = new HttpHandle();
		http.config.setRequestProperty("If-Modified-Since", new Date().toString());
		http.config.setRequestProperty("Cache-Control", "max-age=0");
		http.config.setRequestProperty("Upgrade-Insecure-Requests", "1");
		http.config.setKeepAlive(true);
		http.config.setCookie(cookie);
		HttpEntity entity = http.Get(url);
		if (entity.getCode() != 200) {
			loadCookie(url);
			http.config.setCookie(cookie);
			entity = http.Get(url);
		}
		return entity;
	}

	public void loadCookie(String url) {
		cookie = null;
		HttpEntity entity = initCookie(url);
		while (entity == null) {
			entity = initCookie(url);
		}
	}

	public static void main(String[] args) throws NoSuchMethodException, ScriptException, InterruptedException {
		HttpEntity entity = new RobotHttpHandle().Get("http://torrentkittyla.ws/tk/%E5%A4%8D%E4%BB%87%E8%80%85%E8%81%94%E7%9B%9F3+%E6%97%A0%E9%99%90%E6%88%98%E4%BA%89/1-4-0.html");
		System.out.println(entity.getHtml());
	}
}